#include <func.h>
#include<mysql/mysql.h>

int main()
{
    MYSQL conn;
    MYSQL* pconn = mysql_init(&conn);
    if (mysql_real_connect(&conn, "localhost", "root", "1234", "Clounddisk",0,NULL,0)==NULL){
        fprintf(stderr, "mysql_real_connect() failed\n");        
        mysql_close(pconn);
        return EXIT_FAILURE;
    }
    /* mysql_real_connect(&conn,"localhost","root","1234","Clounddisk",0,NULL,0); */

    MYSQL_STMT* stmt = mysql_stmt_init(&conn);
    if(stmt==NULL){
        printf("(%d, %s)\n", mysql_stmt_errno(stmt),
               mysql_stmt_error(stmt));
        mysql_close(pconn);
        return EXIT_FAILURE;
    }
    //实现插入查询语句的预处理
    //TODO
    const char * query = "SELECT * from user_register WHERE username like ?";
    int ret = mysql_stmt_prepare(stmt,query,strlen(query));
    if(ret){
        printf("(%d, %s)\n", mysql_stmt_errno(stmt),
               mysql_stmt_error(stmt));
        mysql_close(pconn);
        return EXIT_FAILURE;
    }

    ret = mysql_stmt_param_count(stmt);
    if(ret != 1){
        puts("1");
        printf("(%d, %s)\n", mysql_stmt_errno(stmt),
               mysql_stmt_error(stmt));
        mysql_close(pconn);
        return EXIT_FAILURE;
    }
    //设置输入参数

    char user_name[20] = {0};
    MYSQL_BIND bind_insert[1];
    memset(bind_insert,0,sizeof(bind_insert));
    unsigned long len = strlen(user_name);

    bind_insert[0].buffer_type = MYSQL_TYPE_STRING;
    bind_insert[0].buffer = user_name;
    bind_insert[0].length = &len;//TODO:可能要初始化
    bind_insert[0].is_null = 0;

    ret = mysql_stmt_bind_param(stmt,bind_insert);
    if(ret){
        printf("(%d, %s)\n", mysql_stmt_errno(stmt),
               mysql_stmt_error(stmt));
        mysql_close(pconn);
        return EXIT_FAILURE;
    }
    strcpy(user_name,"zhangteng");
    len = strlen(user_name);
    ret = mysql_stmt_execute(stmt);
    if(ret) {
        printf("(%d, %s)\n", mysql_stmt_errno(stmt),
               mysql_stmt_error(stmt));
        mysql_close(pconn);
        return EXIT_FAILURE;
    }

    int id = 0;
    char username[20]={0};
    char cryptpasswd[20]={0};
    char pwd[100]={0};

    MYSQL_BIND bind[4];
    memset(bind,0,sizeof(bind));

    bind[0].buffer_type = MYSQL_TYPE_LONG;
    bind[0].buffer = &id;
    bind[0].buffer_length = sizeof(id);
    bind[0].is_null = 0;

    bind[1].buffer_type = MYSQL_TYPE_VAR_STRING;
    bind[1].buffer =username;
    bind[1].buffer_length = sizeof(username);
    bind[1].is_null = 0;

    bind[2].buffer_type = MYSQL_TYPE_VAR_STRING;
    bind[2].buffer = cryptpasswd;
    bind[2].buffer_length = sizeof(cryptpasswd);
    bind[2].is_null = 0;

    bind[3].buffer_type = MYSQL_TYPE_VAR_STRING;
    bind[3].buffer = pwd;
    bind[3].buffer_length = sizeof(pwd);
    bind[3].is_null = 0;

    ret = mysql_stmt_bind_result(stmt,bind);
    if(ret){
        puts("1");
        printf("(%d, %s)\n", mysql_stmt_errno(stmt),
               mysql_stmt_error(stmt));
        mysql_close(pconn);
        return EXIT_FAILURE;
    }

    //执行操作
    //使用结果集接受
    MYSQL_RES* result = mysql_stmt_result_metadata(stmt);
    //先打印第一行的属性
    int cols = mysql_num_fields(result);
    MYSQL_FIELD *pfiled;
    pfiled = mysql_fetch_field(result);
    for(int i =0;i<cols;i++){
        printf("%s\t",pfiled[i].name);
    }
    printf("\n");

    //预处理时不能用res获取真实数据，防止注入
    /* //在打印剩余数据 */
    /* MYSQL_ROW row = result->current_row; */
    /* while((row = mysql_fetch_row(result))!=NULL){ */
    /*     for(int j=0;j<cols;j++){ */
    /*         printf("%s\t\n",row[j]); */
    /*     } */
    /*     printf("\n"); */
    /* } */

    ret = mysql_stmt_store_result(stmt);
    if(ret){
        puts("3");
        printf("(%d, %s)\n", mysql_stmt_errno(stmt),
               mysql_stmt_error(stmt));
        mysql_close(pconn);
        return EXIT_FAILURE;
    }
    while(1){
        int status = mysql_stmt_fetch(stmt);
        if(status == 1||status == MYSQL_NO_DATA){
            printf("status is %d\n",status );
            break;
           }
        printf("%d\t%s\t%s\t%s\t\n",id,username,
               cryptpasswd,pwd);
    }

    mysql_stmt_free_result(stmt);
    mysql_stmt_close(stmt);
    mysql_close(pconn);
    return 0;
}
